<?php
namespace Home\Controller;
use Think\Controller;
class DoctorController extends AdminAuthController {
    /**
     * 医生列表
     */
    public function showlists() {
        $M_category = M('doctor');
        $category_rows = $M_category->select();
        $this->category_rows = $category_rows;
        $this->display();
    }
    /**
     * 添加医生
     */
    public function add() {
        if(IS_POST){
            $M_category = M('doctor');
            $data       = I('post.');
            $data['password'] = md5(123456);
            $data['addtime'] = date("Y-m-d H:i:s");
            $added_row = $M_category->add($data);
            if ($added_row) {
                $this->success('添加成功', U('showlists'));
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }
    }
    /**
     * 删除医生
     */
    public function del($id) {
        $id = intval($id);
        $M_category = M('doctor');

        $cate_row = $M_category->find($id);
        if (!$cate_row) {
            $this->error('该账号不存在！');
        }

        $deled_cate = $M_category->delete($id);
        if ($deled_cate) {
            $this->success('账号删除成功！');
        } else {
            $this->error('账号删除失败！');
        }
    }
    /**
     * 修改医生信息
     */
    public function mod() {
        $M_category = M('doctor');
        if (IS_POST) {
            $id = I('post.id','','intval');
            $cate_row = $M_category->find($id);
            if (!$cate_row) {
                $this->error('该账号不存在！');
            }
            $cate_row = I('post.');
            $updated_row = $M_category->save($cate_row);
            if ($updated_row){
                $this->success('修改成功', U('showlists'));
            } else {
                $this->error('修改失败');
            }
        } else {
            $id = I('get.id','','intval');
            $cate_row = $M_category->find($id);
            $this->cate_row = $cate_row;
            $this->display();
        }
    }
    /**
     * 医院列表
     */
    public function hospitallist() {
        $M_list = M('hospital');
        $M_referral = M('referral');
        $list = $M_list->select();
        foreach($list as $k=>$v){
            $list[$k]['referral_num'] = $M_referral->where(array('hospital_pre'=>$v['id']))->count();
        }
        $this->list = $list;
        $this->display();
    }
    /**
     * 添加医院
     */
    public function hospitaladd() {
        if(IS_POST){
            $M_list             = M('hospital');
            $data               = I('post.');
            $data['password']   = md5('123456' );
            $data['addtime']    = date("Y-m-d H:i:s");
            //print_r($data);die;
            $is_phone = $M_list->where(array('phone' => $data['phone']))->find();
            if($is_phone){
                $return     = array('code'=>-2,'msg'=>'账号不能重复');
                $this->ajaxReturn($return,'json');
            }
            $added_row          = $M_list->add($data);
            if ($added_row) {
                //$this->success('添加成功', U('hospitallist'));
                $return     = array('code'=>0,'msg'=>'添加成功','url'=>U('Doctor/hospitallist'));
                $this->ajaxReturn($return,'json');
            } else {
                //$this->error('添加失败');
                $return     = array('code'=>-1,'msg'=>'添加失败');
                $this->ajaxReturn($return,'json');
            }
        } else {
            $M_list             = M('province');
            $prolist = $M_list->select();
            $this->assign('prolist',$prolist);
            $this->display();
        }
    }
    /**
     * 修改医院信息
     */
    public function hospitalmod() {
        $M_list = M('hospital');
        if (IS_POST) {
            $id = I('post.id','','intval');
            $cate_row = $M_list->find($id);
            if (!$cate_row) {
                $return     = array('code'=>-3,'msg'=>'该账号不存在');
                $this->ajaxReturn($return,'json');
            }
            $data = I('post.');
            $is_phone = $M_list->where("phone = ".$cate_row['phone']." and id != ".$id)->find();
            //print_r($is_phone);die;
            if($is_phone){
                $return     = array('code'=>-2,'msg'=>'账号不能重复');
                $this->ajaxReturn($return,'json');
            }
            $updated_row = $M_list->save($data);
            if ($updated_row) {
                $return     = array('code'=>0,'msg'=>'修改成功','url'=>U('Doctor/hospitallist'));
                $this->ajaxReturn($return,'json');
            } else {
                $return     = array('code'=>-1,'msg'=>'修改失败');
                $this->ajaxReturn($return,'json');
            }
        } else {
            $id         = I('get.id','','intval');
            $list       = $M_list->find($id);
            $M_list     = M('province');
            $prolist    = $M_list->select();
            $this->assign('prolist',$prolist);
            $this->assign('list',$list);
            $this->display();
        }
    }
    /**
     * 删除医院
     */
    public function hospitaldel($id) {
        $id = intval($id);
        $M_list = M('hospital');

        $cate_row = $M_list->find($id);
        if (!$cate_row) {
            $this->error('该医院不存在！');
        }

        $deled = $M_list->delete($id);
        if ($deled) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
    /**
     * 医院详情
     */
    public function hospitaldetail() {
        $M_list = M('hospital');
        $id     = I('get.id','','intval');
        $list   = $M_list->find($id);
        $M_list     = M('province');
        $prolist    = $M_list->select();
        $this->assign('prolist',$prolist);
        $this->list = $list;
        $this->display();
    }
    /**
     * 搜索医院
     */
    public function search() {
        $M_list = M('hospital');
        $name   = I('name');
        //$strlen = strlen($name);
        /*if($strlen < 6){
            $return     = array('code'=>-2,'data'=>'请输入2个汉字以上！');
            $this->ajaxReturn($return,'json');
        }*/
        $list = $M_list->where("name like '%".$name."%'")->select();
        if (!$list) {
            $return     = array('code'=>-1,'data'=>'该医院不存在！');
            $this->ajaxReturn($return,'json');
        }else{
            $return     = array('code'=>0,'data'=>$list);
            $this->ajaxReturn($return,'json');
        }
    }
    /**
     * 患者列表
     */
    public function patientlist(){
        $M_list = M('patient');
        $list = $M_list->select();
        $this->list = $list;
        $this->display();
    }
    /**
     * 患者详情
     */
    public function patientdetail(){
        $M_list = M('patient');
        $id = I('get.id');
        $list = $M_list->find($id);
        $this->list = $list;
        $this->display();
    }
    /**
     * 患者编辑
     */
    public function patientmod(){
        $M_list = M('patient');
        if (IS_POST) {
            $id = I('post.id','','intval');
            $cate_row = $M_list->find($id);
            if (!$cate_row) {
                $this->error('该患者不存在！');
            }
            $cate_row = I('post.');
            $updated_row = $M_list->save($cate_row);
            if ($updated_row){
                $this->success('修改成功', U('patientlist'));
            } else {
                $this->error('修改失败');
            }
        } else {
            $M_category     = M('category');
            $M_state        = M('state');
            $M_treat        = M('treat');
            $M_check        = M('check');
            $id             = I('get.id','','intval');
            $list           = $M_list->find($id);
            $state          = $M_state->select();
            $treat          = $M_treat->select();
            $check          = $M_check->select();
            $category       = $M_category->select();
            $this->assign(array(
                'list'      => $list,
                'state'     => $state,
                'treat'     => $treat,
                'check'     => $check,
                'category'  => $category
            ));
            $this->display();
        }
    }
    /**
     * 患者删除
     */
    public function patientdel($id){
        $M_list = M('patient');
        $id = intval($id);

        $cate_row = $M_list->find($id);
        if (!$cate_row) {
            $this->error('该医院不存在！');
        }

        $deled = $M_list->delete($id);
        if ($deled) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
}